//
//  Perfect1ViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/17.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "Perfect1ViewController.h"
#import "PerfectView1.h"
#import "CompleteRegViewController.h"
//#import <TLSSDK/TLSHelper.h>
//#import <TLSSDK/TLSLoginHelper.h>

@interface Perfect1ViewController ()//<TLSPwdLoginListener,TLSStrAccountRegListener>

@property(nonatomic,weak)PerfectView1 *perfectView;

@end

@implementation Perfect1ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    PerfectView1 *perfectView = [[PerfectView1 alloc] initWithFrame:self.view.frame];
    [perfectView.nextBtn addTarget:self action:@selector(regist)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:perfectView];
    self.perfectView = perfectView;
    [perfectView mas_remakeConstraints:^(MASConstraintMaker *make) {       
        make.edges.mas_equalTo(self.view);
    }];
}

#pragma mark 注册
-(void)regist{
    
    
    
//    CompleteRegViewController *completeVC = [[CompleteRegViewController alloc] init];
//
//    [self.navigationController pushViewController:completeVC animated:YES];
    
    UserAccount *userAccount = [UserAccount account];

    if (self.perfectView.manBtn.selected) {

        userAccount.Sex = @"0";
    }
    else if (self.perfectView.womanBtn.selected){

        userAccount.Sex = @"1";
    }
    else{

        [MBProgressHUD showText:@"请选择性别"];

        return;
    }

    [UserAccount saveAccount:userAccount];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    [params setValue:userAccount.Name forKey:@"Name"];
    
    [params setValue:userAccount.MasterType forKey:@"MasterType"];

    [params setValue:userAccount.NickName forKey:@"NickName"];

    [params setValue:userAccount.RealName forKey:@"RealName"];

    [params setValue:userAccount.userPassword forKey:@"Pwd"];

    [params setValue:userAccount.CellPhone forKey:@"Cellphone"];

    [params setValue:userAccount.Sex forKey:@"sex"];

    [params setValue:userAccount.Pic forKey:@"Pic"];

    [HTTPTool POSTRaw:Post_User_Register parameters:params success:^(id responseObject) {

        if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0){


            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

            [userDefault setBool:YES forKey:@"isLogin"];

            [userDefault setValue:[responseObject objectForKey:@"Token"] forKey:@"Token"];

            [userDefault setValue:[responseObject objectForKey:@"UserID"] forKey:@"UserID"];

            [userDefault setValue:[responseObject objectForKey:@"TLSToken"] forKey:@"TLSToken"];

            [userDefault setObject:userAccount.userPassword forKey:@"password"];

            [userDefault setObject:userAccount.CellPhone forKey:@"username"];

            [userDefault synchronize];

            NSString *userId = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"TLSToken"]];

            NSString *sign = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"SIG"]];
            
            

            [self loginTim:userId andSign:sign];

        }
        else{

            [MBProgressHUD showText:[responseObject objectForKey:@"Message"]];
        }

    } failure:^(NSError *error) {


    }];
}


#pragma mark 登录腾讯IM
/**
 *  登录腾讯
 */
/**
 *  登录腾讯
 */
-(void)loginTim:(NSString *)userId andSign:(NSString *)sign{
    
    __weak typeof(self) weakSelf = self;
    
    MyTabBarViewController *myTabVC = [[MyTabBarViewController alloc] init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = myTabVC;
    
//    IMALoginParam *loginParam = [[IMALoginParam alloc] init];
//    loginParam.identifier = userId;
//    loginParam.userSig = sign;
//    NSLog(@"loginParam.userSig = %@",loginParam.userSig);
//    loginParam.tokenTime = [[NSDate date] timeIntervalSince1970];
//    
//    [IMAPlatform configWith:loginParam.config];
//    
//    //直接登录
//    [[IMAPlatform sharedInstance] login:loginParam succ:^{
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            CompleteRegViewController *completeVC = [[CompleteRegViewController alloc] init];
//            
//            [weakSelf.navigationController pushViewController:completeVC animated:YES];
//        });
//        
//    } fail:^(int code, NSString *msg) {
//        
//        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//        
//        [userDefault removeObjectForKey:@"Token"];
//        
//        [userDefault removeObjectForKey:@"TLSToken"];
//        
//        [userDefault removeObjectForKey:@"UserID"];
//        
//        [userDefault removeObjectForKey:@"isLogin"];
//        
//        [userDefault synchronize];
//        
//        [MBProgressHUD showError:@"登录失败"];
//    }];
}

@end
